﻿
namespace IInject.Resolution.Activation
{
	public class PropertyDependencyActivator : IPropertyDependencyActivator
	{
		#region Constructors

		public PropertyDependencyActivator(ITypeDependencyActivator typeActivator)
		{
			TypeActivator = typeActivator;
		}

		#endregion

		#region Properties

		protected ITypeDependencyActivator TypeActivator { get; set; }

		#endregion

		#region Methods

		///<summary>
		/// Activates the property for the dependency.
		///</summary>
		///<param name="dependency">The property to activate.</param>
		///<param name="component">The component the property belongs to.</param>
		public void Activate(PropertyDependency dependency, object component)
		{
			var value = dependency.Value;

			if (value == null)
			{
				value = Activate(dependency.Parent);
			}

			dependency.Dependent.SetValue(component, value, null);
		}

		public T Activate<T>(TypeDependency dependency)
		{
			return TypeActivator.Activate<T>(dependency);
		}

		public object Activate(TypeDependency dependency)
		{
			return TypeActivator.Activate(dependency);
		}

		#endregion
	}
}
